package com.example.food.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.food.activity.ProductsettlementActivity;
import com.example.food.bean.AddressList;
import com.example.home.R;
import com.google.gson.Gson;

import java.util.List;

public class ShowAddressAdapter extends RecyclerView.Adapter<ShowAddressAdapter.MyHoldy> {
    private Context context;
    private List<AddressList.DataBean> list;

    public ShowAddressAdapter(Context context, List<AddressList.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adreess,null);
        return new ShowAddressAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        holder.addressDetail.setText(list.get(position).getAddressDetail());
        holder.phone.setText(list.get(position).getPhone());
        holder.name.setText(list.get(position).getName());

        holder.re_goodsdislist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String backData = gson.toJson(list.get(position));
                // 点击时将地址数据传递给 Activity
                if (context instanceof ProductsettlementActivity) {
                    ProductsettlementActivity activity = (ProductsettlementActivity) context;
                    activity.dismissDialog(backData);

                    // 关闭底部弹窗
                    if (activity.bottomSheetDialog != null && activity.bottomSheetDialog.isShowing()) {
                        activity.bottomSheetDialog.dismiss();
                    }
                }
            }
        });

    }




    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        TextView addressDetail;
        TextView phone;
        TextView name;
        ImageView update;
        RelativeLayout re_goodsdislist;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            addressDetail = itemView.findViewById(R.id.addressDetail);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            re_goodsdislist = itemView.findViewById(R.id.re_goodsdislist);
            update = itemView.findViewById(R.id.update);
        }
    }
}
